package com.my.hzb.order.mq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.Topic;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import com.my.hzb.order.ctrl.OrderOptCtrl;

public class MqSender {

	private static final Logger log = LogManager.getLogger(MqSender.class);
	private JmsTemplate jmsTemplate;
	private String codeTopic;
	private Topic topic;
	 
	public void sendMessage(final String message) {
		jmsTemplate.send(new MessageCreator() {
           public Message createMessage(Session session) throws JMSException {
               return session.createTextMessage(message);
           }
       });
//		if(null==jmsTemplate.getDefaultDestinationName()){
//			this.send(message);
//		}else{
//		   log.info("order发送的消息："+message+"，目标队列是："+jmsTemplate.getDefaultDestinationName());
//	       jmsTemplate.send(new MessageCreator() {
//	           public Message createMessage(Session session) throws JMSException {
//	               return session.createTextMessage(message);
//	           }
//	       });
//		}
       log.info("order发送的消息成功");
   }

	public void send(final String message){
		try {
			if(null==codeTopic){
				codeTopic = "hzb_user";
			}
            if (topic == null) {
                topic = jmsTemplate.getConnectionFactory().createConnection()
                        .createSession(false, Session.AUTO_ACKNOWLEDGE)
                        .createTopic(codeTopic);
                log.info("order发送的消息："+message+"，目标队列是："+codeTopic);
                jmsTemplate.send(topic,new MessageCreator() {
                    public Message createMessage(Session session) throws JMSException {
                        return session.createTextMessage(message);
                    }
                });
                topic=null;
            }

        }catch (Exception e){
        	e.printStackTrace();
        }
	}
	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

	public String getCodeTopic() {
		return codeTopic;
	}

	public void setCodeTopic(String codeTopic) {
		this.codeTopic = codeTopic;
	}

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}
}
